package com.feri.fc.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feri.fc.admin.param.PageParam;
import com.feri.fc.admin.service.HospitalService;
import com.feri.fc.dao.HospitalDao;
import com.feri.fc.entity.Hospital;
import com.feri.fc.param.HospitalAdd;
import com.feri.fc.vo.PageData;
import com.feri.fc.vo.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/27 13:57
 * @Modified:
 */
@Service
@RequiredArgsConstructor
public class HospitalServiceImpl implements HospitalService {
    private final HospitalDao dao;

    @Override
    public R add(HospitalAdd add) {
        LambdaQueryWrapper<Hospital> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Hospital::getName,add.getName());
        Hospital hospital=dao.selectOne(wrapper);
        if(hospital!=null){
            return R.fail();
        }
        hospital=new Hospital();
        BeanUtils.copyProperties(add,hospital);
        hospital.setCreateTime(new Date());
        hospital.setUpdateTime(new Date());
        dao.insert(hospital);

        return R.ok();
    }

    @Override
    public PageData<Hospital> queryPage(PageParam param) {
        LambdaQueryWrapper<Hospital> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Hospital::getCreateTime);

        Page<Hospital> page=dao.selectPage(new Page<>(param.getPage(),param.getLimit()),wrapper);

        return PageData.ok(page.getTotal(),page.getPages(),page.getRecords());
    }

    @Override
    public R all() {
        return R.ok(dao.selectList(null));
    }
}
